package com.excesys.exsecs.energy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.RequestExcel;
import com.excesys.exsecs.energy.entity.BumDevEnroll;
import com.excesys.exsecs.energy.service.DevEnrollService;
import com.excesys.exsecs.energy.vo.DevEnrollExcelImportVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 供配电设备注册
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/devEnroll")
@Tag(name = "供配电设备注册")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class DevEnrollController {

	private final DevEnrollService devEnrollService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param bumDevEnroll
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('devEnroll_view')" )
	public R getPage(Page page, BumDevEnroll bumDevEnroll) {
		return R.ok(devEnrollService.page(page,Wrappers.query(bumDevEnroll).orderByDesc("create_time")));
	}

	/**
	 * 新增
	 * @param bumDevEnrollList 多个或单个对象
	 * @return
	 */
	@Operation(summary = "新增", description = "新增")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('devEnroll_add')" )
	public R save(@RequestBody List<BumDevEnroll> bumDevEnrollList) {
		for(BumDevEnroll bumDevEnroll:bumDevEnrollList){
			devEnrollService.save(bumDevEnroll);
		}
		return R.ok();
	}

	/**
	 * 修改
	 * @param bumDevEnroll
	 * @return
	 */
	@Operation(summary = "修改", description = "修改")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('devEnroll_edit')" )
	public R edit(@RequestBody BumDevEnroll bumDevEnroll) {
		return R.ok(devEnrollService.updateById(bumDevEnroll));
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Operation(summary = "删除", description = "删除")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('devEnroll_del')" )
	public R del(@PathVariable Long id) {
		return R.ok(devEnrollService.removeById(id));
	}

	/**
	 * 导入设备
	 */
	/**
	 * 导入点位
	 * @param excelVOList 点位列表
	 * @param bindingResult 错误信息列表
	 * @return ok fail
	 */
	@PostMapping("/import")
	@PreAuthorize("@pms.hasPermission('devEnroll_import')" )
	public R importRole(@RequestExcel List<DevEnrollExcelImportVO> excelVOList, BindingResult bindingResult) {
		return devEnrollService.importBamSBuildings(excelVOList, bindingResult);
	}

}